<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cco extends CI_Controller {
	
        public function __construct()
	{
		parent::__construct();
                $this->load->model('cco_model','mb');
                $this->load->model('boq_model','mbb');
                if(!$this->session->userdata('logged_in'))
                {
                  redirect('login');
                }
	}
	
	function ccoList($id="")
	{
		if($id != "")
		{
			$selectedCCO = $this->mb->get($id);
		}
		else
		{
			$selectedCCO = NULL;
		}
		$data['selectedCCO'] = $selectedCCO;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['cco'] = $this->mb->get();
		$data['boqs'] = $this->mbb->get();
		$this->load->view('cco/index', $data);
	}
	
	function saveCCO()
	{
		$this->form_validation->set_rules('pay_item','Pay Item','required');
		$this->form_validation->set_rules('keterangan','Keterangan','required');
		$this->form_validation->set_rules('jumlah','Jumlah','required');
		$this->form_validation->set_rules('no_mc','Harga Satuan','required');
		$this->form_validation->set_rules('no_cco','Harga Satuan','required');
		$this->form_validation->set_rules('harga_satuan','Harga Satuan','required');
		
                $this->form_validation->set_message('required','Kolom %s harus diisi dengan benar');
                
                
            if ($this->form_validation->run())
            {
				if($this->input->post('id_cco')!="")
				{
					$id = $this->input->post('id_cco');
				}
				$ccoData = array(
					'pay_item' => $this->input->post('pay_item'),
					'keterangan' => $this->input->post('keterangan'),
					'jumlah' => $this->input->post('jumlah'),
					'no_mc' => $this->input->post('no_mc'),
					'nomor_cco' => $this->input->post('no_cco'),
					'tanggal_cco' => date('Y-m-d'),
					'harga_satuan' => $this->input->post('harga_satuan')
					);
				
				$config['upload_path'] = './assets/fileCCO/';
				$config['allowed_types'] = '*';
				$config['max_size'] = '100000';
		
				if($this->mb->is_cco_exist($id) == true)
				{
					if($this->mbb->is_boq_exist($ccoData['pay_item'])== true)
					{
					//Update
						$config['file_name'] = $this->mb->getFile($id);
						$config['overwrite'] = TRUE;
						$this->load->library('upload', $config);
			
						if (!$this->upload->do_upload('cco_file') and !empty($_FILES['cco_file']['name']))
						{
							$this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>'.$this->upload->display_errors().'</p>');
							redirect('cco/ccoList');
						}
						
						$this->mb->update($id, $ccoData);
						$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$ccoData['cco'].'</b> berhasil di ubah!</p>');
						redirect('cco/ccoList');
					}
					else
					{
						die('false');
						$this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Pay Item Tidak Ditemukan!</p>');
						redirect('cco/ccoList');
					}
				}
				else
				{
					//Add
					if($this->mbb->is_boq_exist($ccoData['pay_item'])== true)
					{
						$config['file_name'] = 'cco_'.do_hash(date("Ymd").time(), 'md5');
						$this->load->library('upload', $config);
			
						if (!$this->upload->do_upload('cco_file') and !empty($_FILES['cco_file']['name']))
						{
							$this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>'.$this->upload->display_errors().'</p>');
							redirect('cco/ccoList');
						}
			
						$file_data = $this->upload->data();
						$ccoData['cco_file'] = $config['file_name'].$file_data['file_ext'];
						
						$this->mb->insert($ccoData);
						$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$boqData['boq'].'</b> berhasil di tambah!</p>');
						redirect('cco/ccoList');
					}
					else
					{
						$this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Pay Item Tidak Ditemukan!</p>');
						redirect('cco/ccoList');
					}
				}
            }
                else
                {
                        $this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Isi form dengan benar!</p>');
						redirect('cco/ccoList');
                }        
	}
	
	function deleteCCO($id_cco="")
	{
		if($this->mb->is_cco_exist($id_cco) == true)
		{
			$this->mb->delete($id_cco);
			$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$pay_item.'</b> berhasil di hapus!</p>');
			redirect('cco/ccoList');
		}
		else
		{
			$this->session->set_flashdata('msg', '<p class="alert alert-danger"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Tidak bisa hapus diri sendiri!</b></p>');
			redirect('cco/ccoList');
		}
	}
}